﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XPMVC.Controllers;
using XPMVC.Database;
using XPMVC.Models;

namespace XPMVC
{
    public partial class Items : System.Web.UI.Page
    {
        int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.QueryString["ID"]);

            if (id > 0)
            {
                ArtPiece item = null;

                using (xpEntities db = new xpEntities())
                {
                    item = db.ArtPiece.Where(x => x.id.Equals(id)).FirstOrDefault();

                    if ((item != null) && item.available)
                    {
                        BidHistory(id);

                        name.Text = item.name;
                        image.ImageUrl = item.picture;
                        description.Text = item.description;
                        author.Text = item.author;
                        location.Text = item.location;
                    }
                    else
                    {
                        error.Visible = true;
                        table.Visible = false;
                    }
                }
            }
            else
            {
                Response.Redirect("Catalogue.aspx");
            }
        }

        protected void btnBid_Click(object sender, EventArgs e)
        {
            xpEntities db = new xpEntities();

            BiddingRules br = new BiddingRules();

            try
            {
                int user_id = Convert.ToInt32(Session["id"]);
                int bid = Int32.Parse(txtBid.Text.Length > 0 ? txtBid.Text : "0");
                int points = db.Member.Where(x => x.id.Equals(user_id)).FirstOrDefault().points ?? 0;

                message.Visible = true;

                if (br.valid(bid, points))
                {
                    AuctionController au = new AuctionController();

                    if (au.Bet(id, bid, Int32.Parse(Session["id"].ToString())))
                    {
                        BidHistory(id);

                        message.InnerText = "Bid is placed.";
                        message.Style.Add("color", "green");
                    }
                    else
                    {
                        message.InnerText = "Your bid is not valid. It must be more than the current bid.";
                        message.Style.Add("color", "red");
                    }
                }
                else
                {
                    message.InnerText = "Your bid is not valid. It must be a number between 0 and " + (points * 3);
                    message.Style.Add("color", "red");
                }
            }
            catch(Exception)
            {
                message.InnerText = "You have to put a number as bid";
                message.Style.Add("color", "red");
            }

            db.Dispose();
        }

        private void BidHistory(int id)
        {
            using (xpEntities db = new xpEntities())
            {
                var cb = db.Bidding.Where(x => x.art_piece.Equals(id)).OrderByDescending(x => x.id).FirstOrDefault();

                if (cb == null)
                {
                    current_bid.Text = "0";
                    current_bidder.Text = "n/a";
                }
                else
                {

                    var bid_history =
                                           from b in db.Bidding
                                           join m in db.Member on b.bider equals m.id
                                           where b.art_piece.Equals(id)
                                           orderby b.id descending
                                           select new
                                           {
                                               bid_date = b.bid_date,
                                               current_bid = b.current_bid,
                                               bider = m.username
                                           };

                    bids.DataSource = bid_history.ToList();
                    bids.DataBind();

                    var cb_bidder = db.Member.Where(x => x.id.Equals(cb.bider)).FirstOrDefault();

                    current_bid.Text = cb.current_bid.ToString();
                    current_bidder.Text = cb_bidder.username;
                }
            }
        }
    }
}